package me.spring.cloud.common.components.oss.cloud.qcloud;


import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.request.UploadFileRequest;
import com.qcloud.cos.sign.Credentials;
import me.spring.cloud.common.components.util.io.StreamUtil;
import me.spring.cloud.common.components.util.lang.StringUtil;
import me.spring.cloud.common.components.oss.cloud.AbstractCloudStoreServiceImpl;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.validation.constraints.NotNull;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * 腾讯云存储
 *
 * @author huangzhibin
 * @date 2018-07-25 20:51
 */
public class QcloudCloudStorageServiceImpl extends AbstractCloudStoreServiceImpl {

  private final static String UP_SUCCESS = "0";

  private COSClient client;

  private ObjectMapper objectMapper = new ObjectMapper();


  public QcloudCloudStorageServiceImpl(QcloudConfig config) {
    super.setConfig(config);
    Credentials credentials = new Credentials(config.getQcloudAppId(), config.getQcloudSecretId(),
        config.getQcloudSecretKey());

    //初始化客户端配置
    ClientConfig clientConfig = new ClientConfig();
    //设置bucket所在的区域，华南：gz 华北：tj 华东：sh
    clientConfig.setRegion(config.getQcloudRegion());
    client = new COSClient(clientConfig, credentials);
  }

  public String uploadLocal(String file, String fileName) throws Exception {
    return null;
  }

  public String upload(byte[] data, String suffix) throws Exception {
    String path = this.getPath(super.getConfig().getPrefix(), suffix);
    //腾讯云必需要以"/"开头
    if (!path.startsWith("/")) {
      path = "/" + path;
    }
    //上传到腾讯云
    UploadFileRequest request = new UploadFileRequest(this.getConfig().getBucketName(), path, data);
    String response = client.uploadFile(request);

    QcloudUpResp resp = objectMapper.readValue(response, QcloudUpResp.class);
    if (!UP_SUCCESS.equals(resp.getCode())) {
      throw new RuntimeException("文件上传失败，" + resp.getMessage());
    }
    return StringUtil.append(this.getConfig().getDomain(), path);
  }

  public String upload(InputStream inputStream, String suffix) throws Exception {
    byte[] data = StreamUtil.toByteArray(inputStream);
    return this.upload(data, suffix);
  }

  @Override
  public String uploadForLocal(String file, String fileName) throws Exception {
    return null;
  }

  @Override
  public String uploadForByte(byte[] data, String fileName, String mime) throws Exception {
    return null;
  }

  @Override
  public String uploadForInputStream(InputStream inputStream, String fileName, String mime) throws Exception {
    return null;
  }

  @Override
  public void modifyMimeType(String fileName, String newMimeType) throws Exception {

  }

  @Override
  public HashMap<String, Map<String, Object>> batchDelete(String... fileNames) throws Exception {
    return null;
  }

  @Override
  public String renameFile(String fileName, String newFileName) throws Exception {
    return null;
  }

  @Override
  public void delete(String fileName) throws Exception {

  }

  @Override
  public boolean isExists(@NotNull String file) throws Exception {
    return false;
  }
}
